<?php
namespace Resource\Model;
use Think\Model;
use Common\ORG\Util\AuthTable;

/**
 * 客户
 * @author Evan
 * @since 2016年5月11日
 */
class CustomerModel extends Model {
	protected $trueTableName = 'wz_customers';

    /**
     * @param $params
     * @param string $fields
     * @return mixed
     */
	public function getCustomerInfo($params, $fields = '') {
		if($fields) {
			$userInfo = $this->where($params)->field($fields)->order('create_time DESC')->find();
		} else {
			$userInfo = $this->where($params)->order('create_time DESC')->find();
		}
		return $userInfo;
	}

    /**
     * @param $params
     * @return mixed
     */
	public function addCustomer($params) {
		$user_id = $this->add($params);
		return $user_id;
	}

    /**
     * @param $params
     * @return bool
     */
	public function saveCustomer($params) {
		$result = $this->save($params);
		if($result !== false) {
			return true;
		} else {
			return false;
		}
	}

    /**
     * @param $params
     * @return mixed
     */
    public function getCustomerCount($params) {
        return $this->where($params)->count();
    }

    /**
     * 查询列表
     * @param $params
     * @param int $page
     * @param int $page_size
     * @return array
     */
    public function getCustomerList($params, $page=1, $page_size=20) {
        $dataList = $this->where($params)->order('update_time DESC')->limit($page_size)->page($page)->select();
        return $dataList ? $dataList : [];
    }
    /**
     * @param $params
     * @return mixed
     */
    public function getCommonCustomerCount($params) {
        return M('wz_customers')->alias('wc')
            ->field('wc.*,ad.name AS department_name')
            ->join("LEFT JOIN tb_auth_user AS au ON au.id=wc.admin_id")
            ->join("LEFT JOIN tb_auth_department AS ad ON au.department_id=ad.id")
            ->where($params)
            ->count();
    }

    /**
     * 查询列表
     * @param $params
     * @param int $page
     * @param int $page_size
     * @return array
     */
    public function getCommonCustomerList($params, $page=1, $page_size=20) {
        $dataList = M('wz_customers')->alias('wc')
            ->field('wc.*,ad.name AS department_name')
            ->join("LEFT JOIN tb_auth_user AS au ON au.id=wc.admin_id")
            ->join("LEFT JOIN tb_auth_department AS ad ON au.department_id=ad.id")
            ->where($params)
            ->order('wc.update_time DESC')
            ->limit($page_size)
            ->page($page)
            ->select();
        return $dataList ? $dataList : [];
    }

    /**
     * all
     * @param $params
     * @param string $fields
     * @return array|mixed
     */
    public function getAllCustomerList($params, $fields='') {
        if($fields) {
            $dataList = $this->where($params)->field($fields)->select();
        } else {
            $dataList = $this->where($params)->select();
        }
        return $dataList ? $dataList : [];
    }

    /**
     * 根据公司查询客户总数
     * @param $params
     * @return mixed
     */
    public function getCustomerCountByCompany($params) {
            $sql = M('wz_customers')->alias('wc')
            ->field('wc.*,ad.name AS department_name')
            ->join("LEFT JOIN tb_auth_user AS au ON au.id=wc.admin_id")
            ->join("LEFT JOIN tb_auth_department AS ad ON au.department_id=ad.id")
            ->where($params)
            ->group('wc.company_name')
            ->order('wc.company_name,wc.update_time DESC')
            ->buildSql();

            $model = new Model();
        return $model->table($sql.' a')->count();
    }

    /**
     * 根据公司查询客户列表
     * @param $params
     * @param int $page
     * @param int $page_size
     * @return array
     */
    public function getCustomerListByCompany($params, $page=1, $page_size=20) {
        $dataList = M('wz_customers')->alias('wc')
            ->field('wc.*,ad.name AS department_name')
            ->join("LEFT JOIN tb_auth_user AS au ON au.id=wc.admin_id")
            ->join("LEFT JOIN tb_auth_department AS ad ON au.department_id=ad.id")
            ->where($params)
            ->group('wc.company_name')
            ->order('wc.company_name,wc.update_time DESC')
            ->limit($page_size)
            ->page($page)
            ->select();
        return $dataList ? $dataList : [];
    }
}